package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsAdminStrongService;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/storage")
public class DtsAdminStrongController {
    @Autowired
    DtsAdminStrongService strongService;
    @RequestMapping("/update")
    public Object update(@RequestBody DtsStorage storage){
        strongService.update(storage);
        return ResponseUtil.ok();

    }


    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        strongService.deleteById(storage);
        return ResponseUtil.ok();
    }


    @RequestMapping("/create")
    public Object create(MultipartFile file) throws Exception {
        //获取文件的字节数
        byte[] bytes = file.getBytes();
        //生成文件名uuid
        String uuid = UUID.getRandom().toString();
        //获取原始的文件名
        String name = file.getName();
        String key = uuid + name;
        DtsStorage storage = strongService.add(bytes,key,name,(int)file.getSize(),file.getContentType());
        return ResponseUtil.ok(storage);
    }


    @RequestMapping("/list")
    public Object list(
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "limit", required = false, defaultValue = "10") Integer limit,
            String sort,String order,String key,String name){
        IPage<DtsStorage> iPage =  strongService.findBYpage(page,limit,sort,order,key,name);
        Map<String,Object> date = new HashMap<>();
        date.put("total",iPage.getTotal());
        date.put("items",iPage.getRecords());
        return ResponseUtil.ok(date);
    }
}
